const config = require('utils/config.js')
var commJs = require('utils/common.js');

App({
  onLaunch() {
    // 静默登录获取用户 unionid、openid 等信息
    // this.UserLogin("noUser");
    this.UserLogin("");
  },
  // 生命周期函数--监听页面显示
  onShow() {
    console.log("isCheckUpdate："+ this.globalData.isCheckUpdate);
    // if (this.globalData.isCheckUpdate == 1){
        this.CheckUpdate();
    // }
  },

  // 用户登录
  UserLogin(mode){
    this.globalData.errMsg = '';
    var that = this;
    wx.login({
        success: res => {
            if (res.code) {
                console.log("程序初始化：会员登录请求成功")
                console.log(res)
                // 发起网络请求
                wx.request({
                    url: getApp().globalData.apiUrl,
                    method: "POST",
                    header:{"content-type" :'application/x-www-form-urlencoded'},
                    data: {
                        action: "login",
                        updateTime: getApp().globalData.updateTime,
                        siteType: getApp().globalData.siteType,
                        siteId: getApp().globalData.siteId,
                        apiID: getApp().globalData.siteId,
                        mode: mode,
                        code: res.code,
                        sign: "123456"
                    },
                    // success: function(res){
                    success: res => {
                        if (res.statusCode == 200){
                            let apiData = res.data;
                            var dbArr = apiData.data;
                            console.log("登录成功", apiData);
                            if (apiData.res){
                                console.log("会员登录成功反馈信息：");
                                console.log(apiData);
                                var globalData = getApp().globalData;
                                var appUser = globalData.user;
                                appUser.id = apiData.userID;
                                appUser.unionid = apiData.unionid;
                                appUser.openid = apiData.openid;
                                appUser.nickname = apiData.userNick;
                                // appUser.img = apiData.userImg;
                                // appUser.key = apiData.userKey;
                                // appUser.key = apiData.key;
                                globalData.isCheckUpdate = apiData.isCheckUpdate;
                                
                                console.log("程序初始化：会员登录成功");
                                console.log(globalData);
                                appUser.isLogin = true;
                            }else{
                                let errStr = "获取会员信息出错"+ apiData.note;
                                getApp().globalData.errMsg = errStr;
                                // commJs.ErrLog(errStr, res);
                                wx.showModal({
                                    title: "获取信息失败",
                                    content: errStr,
                                    showCancel: false,
                                    confirmText: "重试",
                                    confirmColor: "red",
                                    success: function(res) {
                                        if (res.confirm) {
                                            console.log('用户点击确定');
                                            that.UserLogin(mode);
                                        }
                                    }
                                });
                            }
                        }else{
                            let errStr = "程序初始化：获取会员信息状态错误"+ res.statusCode;
                            getApp().globalData.errMsg = errStr;
                            commJs.ErrLog(errStr, res)
                        }
                        // 所以此处加入 callback 以防止这种情况
                        if (this.checkLoginReadyCallback){
                            this.checkLoginReadyCallback(res);
                        }
                    },
                    fail: function(res){
                        getApp().globalData.errMsg = "程序初始化：获取信息失败"+ JSON.stringify(res);
                        commJs.ErrLog("程序初始化：获取信息失败", res)
                    },
                    complete: function(res){
                        /* console.log("程序初始化：获取server信息操作完成")
                        console.log(res) */
                    }
                });
            } else {
                getApp().globalData.user.isLogin = false;
                let errStr = "程序初始化：会员登录请求失败"+ res.errMsg;
                getApp().globalData.errMsg = errStr;
                commJs.ErrLog(errStr, res)
            }
            // 发送 res.code 到后台换取 openId, sessionKey, unionId
        }
    })
  },
  // 检查是否有新版本
  CheckUpdate: function (){
    console.log('检查是否有新版本');
    const updateObj = wx.getUpdateManager();
    updateObj.onCheckForUpdate(function(res) {
        // 请求完新版本信息的回调
        console.log("请求完新版本信息的回调："+ res.hasUpdate);
    });
    updateObj.onUpdateReady(function (){
        wx.showModal({
          title: '更新提示',
          content: '新版本已经准备好，是否重启应用？',
          success: function (res){
              if (res.confirm){
                console.log('更新提示，用户点击[确定]');
                // 调用 applyUpdate 应用新版本并重启
                updateObj.applyUpdate();
              }else if (res.cancel) {
                console.log('更新提示，用户点击[取消]');
              }
          }
        });
    });
    updateObj.onUpdateFailed(function (){
        // 新版本下载失败
        console.log('新版本下载失败');
        /* 
        wx.showModal({
          title: '新版本下载失败',
          content: '新版本下载失败，请删除当前小程序，重新搜索进入'
        })
        */
    });
  },
  globalData: config
})